package com.example.sangong.utils;

import cn.hutool.core.collection.CollectionUtil;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;

public class MyStreamUtils {

    public static <K,V> Map<K,V> cosMap(Collection<V> collection, Function<V,K> key){
        Map<K,V>result = new HashMap<>();
        if(CollectionUtil.isEmpty(collection)){
            return result;
        }
        for (V v : collection) {
            result.put(key.apply(v), v);
        }
        return result;
    }
    public static <T,R>List<R> cosList(Collection<T> collection, Function<T,R> function){
        List<R> result = new ArrayList<>();
        if(CollectionUtil.isEmpty(collection)){
            return result;
        }
        for (T t : collection) {
         result.add(function.apply(t));
        }
        return result;
    }
    public static <T,R>Set<R> cosSet(Collection<T> collection, Function<T,R> function){
        Set<R> result = new HashSet<>();
        if(CollectionUtil.isEmpty(collection)){
            return result;
        }
        for (T t : collection) {
            result.add(function.apply(t));
        }
        return result;
    }
    public static <V> List<V> filter(Collection<V> collection, Predicate<V> predicate){
        List<V> result = new ArrayList<>();
        if(CollectionUtil.isEmpty(collection)){
            return result;
        }
        for (V v : collection) {
            if(predicate.test(v)){
                result.add(v);
            }

        }
        return result;
    }
}
